package simpleftp.server;

import java.io.File;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
 
 
public class Tree {
	private JTree arbre;
	private DefaultMutableTreeNode racine;
 
	public Tree(File fil){
		listRoot(fil);
	}
 
private void listRoot(File fil ){
	
	    this.racine = new DefaultMutableTreeNode(fil); 
		listFile(fil, this.racine);
		arbre = new JTree(this.racine);
		arbre.setRootVisible(false);
		arbre.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
	}
 
	private DefaultMutableTreeNode listFile(File file, DefaultMutableTreeNode node){
		if(file.isFile())
			return new DefaultMutableTreeNode(file.getName());
		else{
			for(File nom : file.listFiles()){
					DefaultMutableTreeNode subNode;
					if(nom.isDirectory()){
						subNode = new DefaultMutableTreeNode(nom.getName()+"\\");
						node.add(this.listFile(nom, subNode));
					}else{
						subNode = new DefaultMutableTreeNode(nom.getName());
					}
 
					node.add(subNode);
 
			}
			return node;
		}
	}

	public JTree getArbre() {
		return arbre;
	}

	public DefaultMutableTreeNode getRacine() {
		return racine;
	}
	
 
}